/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mygame.Backbone;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import mygame.Manager;
import mygame.Model.ModelManager;
import mygame.Object.GameObject;

/**
 *
 * @author Tobi
 */
public class SavingHandler {

    private String filename = System.getProperty("user.home") + "\\Save\\save.bit";
    private String[] load;
    //[0]-->ObjectPlacement
    private int objectplacement = 0;
    //[1]-->Inventory
    private int inventory = 1;
    //[2]-->Player Position
    private int playerposition = 2;
    //[3]-->TriangleList
    //private int trianglelist = 3;

    public void save() {
        if (Manager.GetInstance().getGameMode() >= GlobalVars.GAME_MODE_PLAYING) {
            try {
                BufferedWriter s = makeSaveBuffer();
                String save = "";

                //Do saving here:
                save += save_ObjectPlacement();
                save += save_Inventory();
                //save += save_PlayerPosition();
                //save += save_TriangleList();
                
                s.write(save);
                s.close();
            } catch (IOException x) {
                System.err.println(x.getMessage());
            }
        }
    }

    public boolean load() {
        boolean loaded = false;
        try {
            BufferedReader l = makeLoadBuffer();

            //loading from file here:
            load = new String[3];
            load[objectplacement] = l.readLine();
            load[inventory] = l.readLine();
            load[playerposition] = l.readLine();
            //load[trianglelist] = l.readLine();

            l.close();
            loaded = true;
        } catch (IOException x) {
            System.err.println(x.getMessage());
            loaded = false;
        }
        System.out.println("Successfully loaded from file!");
        return loaded;
    }

    private String save_ObjectPlacement() {
        String s = "";
        for (Integer k : ModelManager.InteractiveObjects.keySet()) {
            s += ModelManager.InteractiveObjects.get(k).save();
        }
        s += GlobalVars.DELIMITER_LINE;
        return s;
    }

    public void load_ObjectPlacement() {
        if (load[objectplacement] != null) {
            String[] kl = load[objectplacement].split(GlobalVars.DELIMITER_OBJECTS);
            for (int x = 0; x < kl.length; x++) {
                if (!kl[x].equals("")) {
                    GameObject.load(kl[x]);
                }
            }
        }
    }

    private String save_Inventory() {
        return Manager.GetInstance().getPlayer().getInventory().save() + GlobalVars.DELIMITER_LINE;
    }

    public void load_Inventory() {
        if (load[inventory] != null) {
            Manager.GetInstance().getPlayer().getInventory().load(load[inventory]);
        }
    }

//    private String save_PlayerPosition() {
//        return Manager.GetInstance().getMyCam().savePlayerPosition() + GlobalVars.DELIMITER_LINE;
//    }
//
//    public void load_PlayerPosition() {
//        if (load[playerposition] != null) {
//            Manager.GetInstance().getMyCam().loadPlayerPosition(load[playerposition]);
//        }
//    }

    
    private BufferedWriter makeSaveBuffer() throws IOException {
        String fname = filename.substring(0, filename.lastIndexOf("\\"));
        File f = new File(fname);
        f.mkdirs();
        f = new File(filename);
        FileWriter os = new FileWriter(f);
        BufferedWriter b = new BufferedWriter(os);
        return b;
    }

    private BufferedReader makeLoadBuffer() throws IOException {
        FileReader r = new FileReader(new File(filename));
        BufferedReader b = new BufferedReader(r);
        return b;
    }
    
    
    
}
